﻿using System;
using System.Collections;
using System.Collections.Generic;
using Gameplay.PVE;
using Gameplay.PVE.Utils;
using OWL.Rendering.HRP;
using UnityEngine;
using Yoozoo.HRP.Runtime;

public class RpgBattleSceneEnemyPath : MonoBehaviour
{
    public List<Vector3> path = new List<Vector3>();
    public List<int> pathType = new List<int>();
    public List<GameObject> points = new List<GameObject>();
    
    
    
    public void GeneratePath()
    {
        //去空
        for (int i = 0; i < points.Count; i++)
        {
            if (points[i] == null)
            {
                points.RemoveAt(i);
                i--;
            }
        }
        path.Clear();
        pathType.Clear();
        List<List<Vector3>> pathList = new List<List<Vector3>>();
        List<int> type = new List<int>();
        bool isNew = true;
        int lastState = -1;
        for (int i = 0; i < points.Count; i++)
        {
            bool isOver = points[i].name.ToLower().Contains("over");
            var state = isOver ? 1 : 0;
            if (state != lastState)
            {
                isNew = true;
                lastState = state;
            }

            var lastIndex = pathList.Count - 1;
            if (pathList.Count > lastIndex && lastIndex >= 0)
            {
                pathList[lastIndex].Add(points[i].transform.position);
            }
            
            if (isNew)
            {
                isNew = false;
                var list = new List<Vector3>();
                pathList.Add(list);
                type.Add(state);
                list.Add(points[i].transform.position);
            }
        }
        var smoothModifier = new SmoothModifier();
        for (int i = 0; i < pathList.Count; i++)
        {
            var l = pathList[i];
            var isOver = type[i] == 1;
            if (!isOver)
            {
                smoothModifier.SmoothSimple(ref l);
                for (int j = 0; j < l.Count; j++)
                {
                    path.Add(l[j]);
                    pathType.Add(type[i]);
                }
            }
            else
            {
                path.Add(l[0]);
                pathType.Add(1);
            }
        }
    }
    
    public void AddPoint()
    {
        var gameObject = new GameObject("Point");
        gameObject.transform.SetParent(transform);
        if (points.Count > 0)
        {
            gameObject.transform.localPosition = points[points.Count - 1].transform.localPosition;
        }
        else
        {
            gameObject.transform.localPosition = Vector3.zero;
        }
        points.Add(gameObject);        
    }
    
    public void AttachGround()
    {
        for (int i = 0; i < points.Count; i++)
        {
            var newPosition = PveScene.GetAttachGroundPoint(points[i].transform.position);
            points[i].transform.position = newPosition;
        }
    }

    private void OnDrawGizmos()
    {
        for (int i = 0; i < points.Count; i++)
        {
            if (points[i] != null && points[i].transform != null)
            {
                Gizmos.DrawSphere(points[i].transform.position,0.3f);
                if (i < points.Count - 1)
                {
                    if (points[i+1] != null && points[i+1].transform != null)
                    {
                        Gizmos.DrawLine(points[i].transform.position,points[i+1].transform.position);
                    }
                }
            }
        }
    }
}
